package com.lry.web.fiction.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.web.fiction.entity.FictionContent;
import com.lry.web.fiction.mapper.FictionContentMapper;
import com.lry.web.fiction.service.FictionContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class FictionContentServiceImpl implements FictionContentService {


    @Autowired
    FictionContentMapper fictionContentMapper;


    public Map<String, Object> getFictionContent(String chapterId) throws IOException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id",chapterId);
        List<FictionContent> f =fictionContentMapper.selectList(wrapper);

        //对象转换为map
        Map<String, Object> data =new HashMap<String,Object>();
        if(f.size()!=0){
        String strMap = JSONObject.toJSONString(f.get(0));
        JSONObject obj= JSON.parseObject(strMap);
        Iterator it =obj.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            data.put(entry.getKey(), entry.getValue());
        }
        //String转list
        String content = (String) data.get("content");
        List<String> contentToList= Arrays.asList(content.replace("[","").replace("]","").split(","));
        data.put("content",contentToList);
        return data;
        }else{
            data.put("content",new ArrayList<String>());
            return data;
        }

    }




    public int deleteByFictionId(String fictionId) throws Exception {
        List<String> list=  Arrays.asList(fictionId.split(","));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("fiction_id",list);

        return fictionContentMapper.delete(wrapper);
    }

    public int deleteByFVId(String fVId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fiction_variable_id",fVId);

        return fictionContentMapper.delete(wrapper);
    }


}
